﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>

      <% 
          DateTime? startDate = (DateTime?)Model;
          DateTime? endDate = (DateTime?)ViewData["EndDate"];

          string startDateValue = startDate == null ? "" : startDate.Value.ToString("dd-MM-yyyy");
          string endDateDateValue = endDate == null ? "" : endDate.Value.ToString("dd-MM-yyyy");
          
            ModelMetadata meta = ViewData.ModelMetadata;
            string startDateName = meta.PropertyName;
            string endDateName = meta.AdditionalValues["EndDate"].ToString();
            
                %>
               <script type="text/javascript">
                   $(function () {
                       var dates = $("#<%= startDateName%>, #<%= endDateName%>").datepicker({
                           defaultDate: "+1w",
                           changeMonth: true,
                           numberOfMonths: 3,
                           onSelect: function (selectedDate) {
                               var option = this.id == "<%= startDateName%>" ? "minDate" : "maxDate",
		instance = $(this).data("datepicker"),
		date = $.datepicker.parseDate(
			instance.settings.dateFormat ||
			$.datepicker._defaults.dateFormat,
			selectedDate, instance.settings);
                               dates.not(this).datepicker("option", option, date);
                           }
                       });

                       dates.datepicker("option", "dateFormat", "dd-mm-yy");

                       $('#<%= startDateName%>').datepicker("setDate", "<%= startDateValue%>");
                       $('#<%= endDateName%>').datepicker("setDate", "<%= endDateDateValue%>");

                   });
</script>

<label for="from">From</label>
<input type="text" id="<%=startDateName %>" name="<%=startDateName %>Value"/>
<label for="to">to</label>
<input type="text" id="<%=endDateName %>" name="<%=endDateName %>Value"/>     